<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$setting=$return['pluginset'];
if($_RQ['op']=='main'){
	if($_RQ['role']){
		$_SESSION['role']=$_RQ['role'];
	}
	$return['ischecklogin']=model('core/core')->checklogin('','',true);
	$_SESSION['memberid']=$this->model('member')->checkmember();
	$return['groups']=pdo_getall('jy_51job_job_group',array('pid'=>0,'status'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu desc,id desc');
	foreach($return['groups'] as &$item){
		$item['cids']=pdo_getall('jy_51job_job_group',array('pid'=>$item['id'],'status'=>1,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu desc,id desc');
	}
	$setting['bankuai1'] =  !empty($setting['bankuai1']) ? $setting['bankuai1'] : $bankuai1;
	$setting['footernav'] = !empty($setting['footernav']) ? $setting['footernav'] : $footernav;
	$return['pcd']=$this->session('pcd')?:array('city'=>'');
	if($return['pcd']){
		$address=$return['pcd']['province'].' '.$return['pcd']['city'];
		$code=pdo_getcolumn('core_area',array('address'=>$address),'parent_code');
	}
	$return['sharedata']=array(
		'title'=>$setting['share_title'],
		'desc'=>$setting['share_desc'],
		'link'=>custom_url('jy_51job/index//code='.$code,true),
		'imgUrl'=>tomedia($setting['share_image']),
	);
	$return['params']=array('isbao'=>-1,'paytype'=>-1,'sex'=>-1,'xueli'=>-1);
	$return['setting']=$setting;
	$return['member']=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('createtime','credit1','credit2','mobile','avatar','nickname'));
	$return['notices']=pdo_getall('core_account_article',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','id desc');
	$return['xueli_type']=$this->xueli_type;
	$return['job_paytype']=$this->job_paytype;
	$return['job_isbao']=$this->job_isbao;
	if(count($setting['welcomehdps'])>0 && $setting['iswelcome']==1){
		$return['iswelcome']=$this->session('iswelcome')?0:1;
		if($return['iswelcome']){
			$return['welcomehdps']=$setting['welcomehdps'];
			$this->session('iswelcome',1);
		}
	}
	set_html('title',$setting['title']?$setting['title'] :'灵活人才');
	exi($return);
}	
// js 获取数据
if($_RQ['op']=='getlist'){
	$post=get_postdata();
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
	);
	if(trim($_RQ['keyword'])){
		$return['params']['keyword']=trim($_RQ['keyword']);
		$merchids=pdo_getall('jy_51job_merch',array('title like'=>'%'.trim($_RQ['keyword']).'%','uniacid'=>$_SESSION['uniacid']),'id','id');
		if($merchids){
			$merchids=array_keys($merchids);
			$params['merchid']=$merchids;
			$count=pdo_count('jy_51job_job',$params);
			if(!$count){
				unset($params['merchid']);
				$params['title like']='%'.trim($_RQ['keyword']).'%';
			}
		}else{
			$params['title like']='%'.trim($_RQ['keyword']).'%';
		}
	}
	if($_RQ['pid']){
		$params['pid']=intval($_RQ['pid']);
		$return['params']['pid']=intval($_RQ['pid']);
	}
	if($_RQ['cid']){
		$params['cid']=intval($_RQ['cid']);
		$return['params']['cid']=intval($_RQ['cid']);
	}
	if($_RQ['sid']){
		$params['sid']=intval($_RQ['sid']);
		$return['params']['sid']=intval($_RQ['sid']);
	}
	if($_RQ['ishot']){
		$params['ishot']=intval($_RQ['ishot']);
	}
	if($_RQ['isbao']>0){
		$params['baotype']=intval($_RQ['isbao']);
	}
	if($_RQ['paytype']>0){
		$params['paytype']=intval($_RQ['paytype']);
	}
	if($_RQ['xueli']>0){
		$params['xueli <=']=intval($_RQ['xueli']);
	}
	if($_RQ['cids']){
		$cids=str_array($_RQ['cids']);
		$params['cid']=$cids;
	}
	$return['params']['jsjuli']=intval($_RQ['jsjuli']);
	$return['params']['ishot']=intval($_RQ['ishot']);
	if($_RQ['priceMin']){
		$params['price >=']=$_RQ['priceMin'];
		$return['params']['priceMin']=intval($_RQ['priceMin']);
	}
	if($_RQ['priceMax']){
		$params['price <=']=$_RQ['priceMax'];
		$return['params']['priceMax']=intval($_RQ['priceMax']);
	}
	if($_RQ['city'] || $_RQ['district']){
		$params2=array('uniacid'=>$_SESSION['uniacid']);
		$_RQ['city']=str_replace('市','',$_RQ['city']);
		$_RQ['district']=str_replace('市','',$_RQ['district']);
		if($_RQ['city']){
			$params2['city like']='%'.$_RQ['city'].'%';
		}
		if($_RQ['district']){
			$params2['district like']='%'.$_RQ['district'].'%';
		}
		$merchids=pdo_getall('jy_51job_merch',$params2,'id','id');
		$merchids=$merchids?array_keys($merchids):-1;
		$params['merchid']=$merchids;
	}
	if($_RQ['city']){
		$return['params']['city']=$_RQ['city'];
	}
	if($_RQ['district']){
		$return['params']['district']=$_RQ['district'];
	}
	//进一步整理筛选条件，商品属性字段是否需 要调整，商品分类如何筛选，orderby如何整理
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	if(intval($_RQ['num'])){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','paixu','ishot','istop','toptime','sex','xueli','title','remark','onlinetime','paytype','baotype','mid','type','price','looks','lat_lng','pay','createtime','province','city','district','merchid');
	$orderby='paixu desc,ishot desc,istop desc,createtime desc,id desc';
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'fields'=>$fields,
		'orderby'=>$orderby,
	);
	if($_RQ['isjuli'] || $_RQ['isitem']){
		if($_RQ['page']>1){
			$list=array();
		}else{
			$list=pdo_getall('jy_51job_job',$params,$fields,'',$orderby);
		}
	}else{
		//print_r($params);exit;
		$list=pdo_getslice('jy_51job_job',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	}
	$lat_lng=$this->session('lat_lng');
	if($_RQ['lat'] && $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
		$return['params']['lat']=$_RQ['lat'];
		$return['params']['lng']=$_RQ['lng'];
	}
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y-m-d H:m:s',$item['createtime']);
			$item['lat_lng']=json_decode($item['lat_lng'],true);
			$item['onlinetime']=json_decode($item['onlinetime'],true);
			if($lat_lng && $item['lat_lng']['lng'] && $item['lat_lng']['lat']){
				$item['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$item['lat_lng']['lng'],$item['lat_lng']['lat']);
				$item['juli']=price($item['juli']/1000,'ini');
			}
			$item['member']=pdo_get('core_members',array('id'=>$item['mid']));
			$item['images']=json_decode($item['images'],true);
			$item['isme']=$item['mid']==$_SESSION['uid'] ? 1 :0;
			$item['merch']=pdo_get('jy_51job_merch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']),array('title','pcd','district','logo'));
			$item['merchname']=$item['merch']['title']?:'';
			$item['merch']['logo']=tomedia($item['merch']['logo']);
			$item['baotypetitle']=$this->job_paytype2[$item['paytype']].'结('.$this->job_baotype[$item['baotype']].'版)';
			$item['xuelititle']=$this->xueli_type2[$item['xueli']];
			$item['sextitle']=$this->sex2[$item['sex']];
			$pcd=json_decode($item['merch']['pcd'],true);
			$item['city']=$item['city']?:$item['merch']['city'];
			$item['city']=$item['city']?:$pcd['city'];
			$item['district']=$item['district']?:$item['merch']['district'];
			$item['district']=$item['district']?:$pcd['district'];
			if($item['istop'] && $item['toptime']<time()){
				pdo_update('jy_51job_job',array('istop'=>0),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$item['istop']=0;
			}
		}
	}
	if($_RQ['isjuli']){
		paixu($list,'juli','asc');
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='getcitys'){
	$citys=cfc('area')->get_citys($_RQ['py'],$_RQ['name']);
	$return['citys']=$citys;
	exi($return);
}
if($_RQ['op']=='getpcd'){
	if(!empty($_RQ['keyword'])){
			$params['title like']='%'.$_RQ['keyword'].'%';
			$return['params']['title']=$_RQ['keyword'];
	}
	$areas=cfc('area')->get_pcd_titlelist($_RQ['keyword']);
	$return['areas']=$areas;
	exi($return);
}
if($_RQ['op']=='uppcd'){
	$post=get_postdata();
	if($post['lat'] || $post['lng']){
		$lat_lng=array('lat'=>$post['lat'],'lng'=>$post['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$this->session('pcd',$post);
	//print_r($this->session('pcd'));exit;
	exi('更新成功！');
}
if($_RQ['op']=='shoucang'){
	$postdata=get_postdata();
	$id = intval($postdata['pid']);
	if(!$id){
		exi('没有找到该信息，请重新选择！','error');
	}
	$shoucang=pdo_get('jy_51job_shoucang',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'itemid'=>$id));
	if($shoucang){
		if($shoucang['isshoucang'] !=1){
			pdo_update('jy_51job_shoucang',array('isshoucang'=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'itemid'=>$id));
			exi('收藏成功');
		}else{
			pdo_update('jy_51job_shoucang',array('isshoucang'=>0),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'itemid'=>$id));
		   exi('取消收藏');
		}
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'itemid'=>$id,
			'mid'=>$_SESSION['uid'],
			'isshoucang'=>1,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_51job_shoucang',$insert);
	}
	exi('收藏成功');
}
if($_RQ['op']=='setmsg'){
	$postdata=get_postdata();
	$item=pdo_get('jy_51job_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($postdata['pid'])));
	$data=array(
		'comment'=>trim($postdata['msg']),
		'mid'=>$_SESSION['uid'],
		'itemid'=>intval($postdata['pid']),
		//'cid'=>intval($postdata['cid']),
		'uniacid'=>$_SESSION['uniacid'],
		'item_mid'=>$item['mid'],
		'typeid'=>$item['type'],
		'status'=>1,
		'createtime'=>time(),
	);
	$msg='评论成功！';
	if($setting['msg_shenhe']){
		$data['status']=0;
		$msg='评论成功,等待审核！';
	}
	pdo_insert('jy_51job_comment',$data);
	exi($msg);
}
if($_RQ['op']=='del'){	
	if(!$_RQ['id']){
		exi('信息不存在哦！','error');
	}	
	pdo_delete('jy_51job_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除成功！');

}
?>